﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


namespace lxwmvc
{
    /// <summary>
    /// TODO:功能:权限操作类
    /// [09.4.10 lxw]<para />
    /// </summary>
    public class powerhelper 
    {
        /// <summary>
        /// 功能:判断是否具有访问制定ID的权限
        /// [09.4.12 lxw]<para />
        /// </summary>
        /// <param name="strPowerID">需要验证的权限ID</param>
        /// <returns>true:代表具有访问此权限</returns>
        public static bool checkpower(string strPowerID)
        {
            //allmenuadmin 全部权限
            return
                   stringhelper.findvalue(pagebase.PowerString, strPowerID, false)
                   || stringhelper.findvalue(pagebase.PowerString, "allmenuadmin", true);
        }

        /// <summary>
        /// 功能:通过权限验证HTML控件是否显示，用于控件的HTML标签里面
        /// [09.4.12 lxw]<para />
        /// </summary>
        /// <param name="strPowerID">需要验证的权限</param>
        /// <returns>返回控件是否显示和隐藏</returns>
        public static string display(string strPowerID)
        {
            //用户具有此控件的访问权限
            if (checkpower(strPowerID))
            {
                return "";
            }

            return " style='display:none!important' ";
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        /// <param name="jueseid">对应数据库角色id  -999代表管理员</param>
        /// <param name="fuzhujuese">辅助角色,多个角色用,号分割</param>
        public static void setpowerstring(string jueseid,string fuzhujuese = "")
        {
            string juese = (jueseid + "," + fuzhujuese).Trim(new char[]{',',' '});

            //设置角色ID
            //cookieshelper.setcookie("jueseid", juese);

            //系统管理员，可以看见所有的权限,如果角色为-999，代表就是系统管理员
            if (jueseid.Contains("-999"))
            {
                string powerstring = "";
                IEnumerable<supermodel> caidans = queryhelper.query<supermodel>("select * from syscaidan ");

                //数据库里面是最全的。
                foreach (var menu in caidans)
                {
                    if (menu.get<string>("biaoshi").isnotnull())
                        continue;

                    powerstring += "," + menu["syscaidanid"];
                }

                //所有的菜单
                foreach (var caidan in applicationhelper.CAIDANS.Values)
                {
                    powerstring += "," + caidan.biaoshi;
                }

                //所有的权限
                foreach (var power in applicationhelper.QUANXIANS.Values)
                {
                    powerstring += "," + power.biaoshi;
                }

                //去掉重复的字符串
                powerstring = powerstring.Trim(new char[] { ',' });
                powerstring = string.Join(",", powerstring.Split(',').Distinct().ToArray());
                //所有的管理员权限
                pagebase.PowerString = powerstring;
            }
            else
            {
                string sql = "select * from sysjuese where sysjueseid in (" + juese + ")";
                var jueses = sqlhelper.query<supermodel>(sql);
                string juesestr = "";
                foreach (var j in jueses)
                    juesestr += "," + j["caidanids"] + "," + j["quanxianids"];

                //去掉重复的字符串
                juesestr = juesestr.Trim(new char[] { ',' });
                juesestr = string.Join(",", juesestr.Split(',').Distinct().ToArray());
                //菜单和权限
                pagebase.PowerString = juesestr;
            }
        }
    }
}
